#include "DrawingCanvas.h"

DrawingCanvas::DrawingCanvas()
{
	x = 0; y = 0; w = 480; h = 272;
	visible = true;
	bgcolor = Color(0,0,0);
	background = Drawing();
	drawings = ArrayList<Drawing>();
	font = osl_curFont;
	fsize = 1.0f;
	foptions = INTRAFONT_ALIGN_LEFT;
	fcolor = Color(0,0,0);
	fbgcolor = Color(0,0,0,0);
}
DrawingCanvas::DrawingCanvas(int _x, int _y, int _w, int _h)
{
	x = _x; y = _y;
	w = _w; h = _h;
	drawings = ArrayList<Drawing>();
	visible = true;
	bgcolor = Color(0,0,0);
	background = Drawing();
	font = osl_curFont;
	fsize = 1.0f;
	foptions = INTRAFONT_ALIGN_LEFT;
	fcolor = Color(0,0,0);
	fbgcolor = Color(0,0,0,0);
}
int DrawingCanvas::addDrawing(Drawing d)
{
	drawings.add(d);
	return drawings.size()-1;
}
Drawing& DrawingCanvas::getDrawing(int num)
{
	return drawings[num];
}
void DrawingCanvas::removeDrawing(int num)
{
	if(num >= 0 && num < drawings.size())
		drawings[num].uncache();
	drawings.remove(num);
}
Drawing& DrawingCanvas::setDrawing(int num, Drawing dval)
{
	if(num >= 0 && num < drawings.size())
		drawings.get(num).uncache();
	drawings.set(num,dval);
	return drawings[num];
}
void DrawingCanvas::setFont(string fsrc)
{
	font = oslLoadFontFile(fsrc.c_str());
	if(!font)
		font = NULL;
}
void DrawingCanvas::setFont(OSL_FONT *_font)
{
	font = _font;
}
void DrawingCanvas::setFontSize(float _fsize)
{
	fsize = _fsize;
}
void DrawingCanvas::setFontOptions(unsigned short int _foptions)
{
	foptions = _foptions;
}
void DrawingCanvas::setFontColor(Color c)
{
	fcolor = c;
}
void DrawingCanvas::setFontBgColor(Color bgc)
{
	fbgcolor = bgc;
}
void DrawingCanvas::setX(int _x)
{
	x = _x;
}
void DrawingCanvas::setY(int _y)
{
	y = _y;
}
void DrawingCanvas::move(int dx, int dy)
{
	x += dx; y += dy;
}
void DrawingCanvas::moveTo(int _x, int _y)
{
	x = _x; y = _y;
}
void DrawingCanvas::moveTo(Location loc)
{
	x = loc.getX(); y = loc.getY();
}
OSL_FONT * DrawingCanvas::getFont()
{
	return font;
}
float DrawingCanvas::getFontSize()
{
	return fsize;
}
unsigned short int DrawingCanvas::getFontOptions()
{
	return foptions;
}
Color DrawingCanvas::getFontColor()
{
	return fcolor;
}
Color DrawingCanvas::getFontBgColor()
{
	return fbgcolor;
}
int DrawingCanvas::getX()
{
	return x;
}
int DrawingCanvas::getY()
{
	return y;
}
Location DrawingCanvas::getLocation()
{
	return Location(x,y);
}
void DrawingCanvas::setWidth(int _w)
{
	w = _w;
}
void DrawingCanvas::setHeight(int _h)
{
	h = _h;
}
void DrawingCanvas::setSize(int _w, int _h)
{
	w = _w; h = _h;
}
void DrawingCanvas::setSize(Size s)
{
	w = s.getWidth(); h = s.getHeight();
}
void DrawingCanvas::setClipping(int _x, int _y, int _w, int _h)
{
	x = _x; y = _y;
	w = _w; h = _h;
}
int DrawingCanvas::getWidth()
{
	return w;
}
int DrawingCanvas::getHeight()
{
	return h;
}
Size DrawingCanvas::getSize()
{
	return Size(w,h);
}
void DrawingCanvas::hide()
{
	visible = false;
}
void DrawingCanvas::show()
{
	visible = true;
}
void DrawingCanvas::setVisibility(bool _visible)
{
	visible = _visible;
}
bool DrawingCanvas::toggleVisibility()
{
	visible = !visible;
	return visible;
}
bool DrawingCanvas::isVisible()
{
	return visible;
}
void DrawingCanvas::setColor(Color c)
{
	bgcolor = c;
}
void DrawingCanvas::setBackground(Drawing bgdrawing)
{
	 background = bgdrawing;
}
Color DrawingCanvas::getColor()
{
	return bgcolor;
}
OSL_COLOR DrawingCanvas::osl_c()
{
	return bgcolor.osl_c();
}
void DrawingCanvas::clear()
{
	for(int i = 0; i < drawings.size(); i++)
		drawings[i].uncache();
	drawings = ArrayList<Drawing>();
}
void DrawingCanvas::redraw()
{
	if(visible)
	{
		OSL_FONT *origfont = osl_curFont;
		if(font != NULL)
			oslSetFont(font);
		oslSetScreenClipping(x,y,x+w,y+h);
		oslDrawFillRect(x,y,x+w,y+h,bgcolor.osl_c());
		background.redraw();
		for(int i = 0; i < drawings.size(); i++)
		{
			oslSetScreenClipping(x,y,x+w,y+h);
			if(font != NULL)
			{
				if(font->fontType == OSL_FONT_INTRA)
					oslIntraFontSetStyle(font,fsize,fcolor.osl_c(),fbgcolor.osl_c(),foptions);
				else
				{
					oslSetBkColor(fbgcolor.osl_c());
					oslSetTextColor(fcolor.osl_c());
				}
				oslSetFont(font);
			}
			drawings[i].redraw();
		}
		oslResetScreenClipping();
		oslSetScreenClipping(0,0,480,272);
		oslSetFont(origfont);
	}
}
string DrawingCanvas::toString()
{
	string rstr = "Object :: DrawingCanvas:\n";
	rstr += "Num of Drawings = "; 
	rstr += intToString(drawings.size()); rstr += ";\n";
	return rstr;
}
